using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Moonflow.Utility.Math
{
    [ExecuteInEditMode]
    public class MFRotationTransfer : MonoBehaviour
    {
        public string shaderProperty;
        public bool linked = false;
        public bool reverseSync = false;
        public Vector3 direction;
        private Transform _trans;
        public Material mat;
        private float _dir = 1f;
        private void OnEnable()
        {
            _trans = GetComponent<Transform>();
            TryGetComponent(out Renderer renderer);
            if (renderer != null)
            {
                mat = renderer.sharedMaterial;
            }
        }

        private void Update()
        {
            if (reverseSync)
            {
                _trans.forward = direction;
            }
            else
            {
                direction = _trans.forward;
            }

            if (linked)
            {
                if (!string.IsNullOrEmpty(shaderProperty))
                {
                    if (mat != null)
                    {
                        mat.SetVector(shaderProperty, direction);
                    }
                    else
                    {
                        Shader.SetGlobalVector(shaderProperty, direction);
                    }
                }
            }
        }

#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            if (Selection.activeGameObject != this.gameObject) return;
            
            foreach (var cam in Camera.allCameras)
            {
                if (cam.cameraType == CameraType.SceneView)
                {
                    _dir = Vector3.Distance(transform.position, cam.transform.position);
                }
            }
            Gizmos.DrawSphere(transform.position, 0.1f * _dir);
            Gizmos.DrawLine(transform.position, transform.position + transform.forward * _dir * 0.2f);
        }
    }
    
    [CustomEditor(typeof(MFRotationTransfer))]
    public class MFRotationTransferEditor : Editor
    {
        private MFRotationTransfer _target;

        private void OnEnable()
        {
            _target = target as MFRotationTransfer;
        }

        public override void OnInspectorGUI()
        {
            // base.OnInspectorGUI();
            using (new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button(_target.linked ? $"链接至{_target.shaderProperty}" : "未链接"))
                {
                    _target.linked = !_target.linked;
                }

                if (GUILayout.Button(_target.reverseSync ? "将脚本方向同步至Transform" : "将Rotation同步至脚本"))
                {
                    _target.reverseSync = !_target.reverseSync;
                }
            }

            using (new EditorGUILayout.VerticalScope("box"))
            {
                _target.mat = EditorGUILayout.ObjectField("目标材质", _target.mat, typeof(Material)) as Material;
                _target.shaderProperty = EditorGUILayout.TextField("Property名", _target.shaderProperty);
                _target.direction = EditorGUILayout.Vector3Field("Direction", _target.direction);
            }
        }
        
#endif
    }
}